home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / brwsr.2 ƒ / do_moves.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-13  |  5.1 KB  |  219 lines  |  [TEXT/KAHL]

  1. /* some functions to respond to various user inputs...
  2.  *  870806-13-... ^z
  3.  */
  4.  
  5. #include <stdio.h>            /* for FILE, printf(), etc. */
  6. #include <strings.h>        /* for strcpy(), etc. */
  7. #include <unix.h>            /* for exit(), etc. */
  8. #include <proto.h>            /* for function prototypes */
  9. #include "brwsr.h"            /* for various definitions */
  10. #include "brwsr.proto.h"    /* for my function prototypes */
  11.  
  12.  
  13. /* function to interpret a command to move around in the current level
  14.  * of the browser display ... handles "+", "-", and "<return>" ... calls
  15.  * routine make_move() to do the real work....
  16.  */
  17.  
  18. void do_move (cmd)
  19.   char cmd[];
  20.   {
  21.     long move;
  22.     extern FILE *doc_file;
  23.     
  24.     if (doc_file == NULL)
  25.       {
  26.         beep ();
  27.         printf ("No file open!\n");
  28.         return;
  29.       }
  30.     
  31.     if (cmd[1] == '\0')
  32.         strcat (cmd, "1");
  33.     move = atol (cmd);
  34.     make_move (move);
  35.     show_current_item ();
  36.   }
  37.  
  38.  
  39. /* function to do the actual moving around in the INDEX or CONTEXT or
  40.  * TEXT displays .... Safety features prevent user from going
  41.  * off the end of the file in either direction....
  42.  * make_move() returns FALSE if it fails to completely execute the move
  43.  * (i.e., if the move would have run off either end of the file) and
  44.  * beeps at the user ... it returns TRUE if the move succeeds....
  45.  */
  46.  
  47. int make_move (n)
  48.   long n;
  49.   {
  50.     int r;
  51.     PTR_REC get_ptr_rec ();
  52.     extern long current_item[], min_item[], max_item[];
  53.     extern int level;
  54.     extern char *subset;
  55.  
  56.     r = TRUE;
  57.     switch (level)
  58.       {
  59.         case INDEX:
  60.             current_item[INDEX] += n;
  61.             if (current_item[INDEX] < min_item[INDEX])
  62.               {
  63.                 current_item[INDEX] = min_item[INDEX];
  64.                 r = FALSE;
  65.               }
  66.             if (current_item[INDEX] > max_item[INDEX])
  67.               {
  68.                 current_item[INDEX] = max_item[INDEX];
  69.                 r = FALSE;
  70.               }
  71.             break;
  72.  
  73.         case CONTEXT:
  74.             if (subset == NULL)
  75.               {
  76.                 current_item[CONTEXT] += n;
  77.                 if (current_item[CONTEXT] < min_item[CONTEXT])
  78.                   {
  79.                     current_item[CONTEXT] = min_item[CONTEXT];
  80.                     r = FALSE;
  81.                   }
  82.                 if (current_item[CONTEXT] > max_item[CONTEXT])
  83.                   {
  84.                     current_item[CONTEXT] = max_item[CONTEXT];
  85.                     r = FALSE;
  86.                   }
  87.               }
  88.             else
  89.                 r = make_subindex_move (n);
  90.  
  91.             current_item[TEXT] = get_ptr_rec (current_item[CONTEXT]);
  92.             break;
  93.  
  94.         case TEXT:
  95.             r = move_in_text (n);
  96.             break;
  97.       }
  98.     
  99.     if (r == FALSE)
  100.         beep ();
  101.     return (r);
  102.   }
  103.  
  104.  
  105. /* move up or down the chosen number of lines of text, and put the result
  106.  * into current_item[TEXT] ... return FALSE if attempted move would have
  107.  * run off the end of the file, non-zero if the move was completed without
  108.  * error....
  109.  */
  110.  
  111. int move_in_text (move)
  112.   register long move;
  113.   {
  114.     int result;
  115.     register long loc;
  116.     extern long current_item[];
  117.     
  118.     result = TRUE;
  119.     loc = current_item[TEXT];
  120.     
  121.     if (move < 0)
  122.         for ( ; move < 0; ++move)
  123.           {
  124.             if (loc <= 0)
  125.               {
  126.                 result = FALSE;
  127.                 break;
  128.               }
  129.             loc = start_of_line (loc - 1);
  130.           }
  131.             
  132.     else if (move > 0)
  133.         for ( ; move > 0; --move)
  134.           {
  135.             loc = next_line (loc);
  136.             if (loc >= max_item[TEXT])
  137.               {
  138.                 result = FALSE;
  139.                 loc = start_of_line (max_item[TEXT]);
  140.                 break;
  141.               }
  142.           }
  143.     
  144.     current_item[TEXT] = loc;
  145.     return (result);
  146.   }
  147.  
  148.  
  149. /* make a move in the working subindex ... must just step along in
  150.  * whichever direction is desired until either hitting the end (in
  151.  * which case the last valid item found in the subindex should be
  152.  * returned as the current one) or finishing the requested move.
  153.  * Return FALSE if hit a wall, TRUE otherwise....
  154.  *
  155.  * This routine is only called when level == CONTEXT ... since
  156.  * in TEXT level we are just moving around in the full text of
  157.  * the document file, and in the INDEX level we count and display
  158.  * even items with zero occurrences in the subindex...
  159.  *
  160.  * This routine is only called when subset != NULL, since if there
  161.  * is no working subset the move becomes trivial arithmetic.
  162.  *
  163.  * This routine is only called when either current_item[CONTEXT]
  164.  * is a good item in the subset already, or when there is a certainty
  165.  * that a good item will be found in the subsequent scan (specifically,
  166.  * when scanning down to find the first good item when called from
  167.  * do_descend () ....).
  168.  */
  169.  
  170. int make_subindex_move (move)
  171.   long move;
  172.   {
  173.       register long i, last_good_item;
  174.     int result;
  175.     PTR_REC get_ptr_rec ();
  176.     extern long current_item[], min_item[], max_item[];
  177.     extern char *subset;
  178.  
  179.     result = TRUE;
  180.     last_good_item = current_item[CONTEXT];
  181.  
  182.     if (move >= 0)
  183.       {
  184.         for (i = 0; i < move; ++i)
  185.           {
  186.             while (++current_item[CONTEXT] <= max_item[CONTEXT] &&
  187.                 ! get_subset_bit ((long)
  188.                     get_ptr_rec (current_item[CONTEXT])));
  189.             if (current_item[CONTEXT] > max_item[CONTEXT])
  190.                 break;
  191.             last_good_item = current_item[CONTEXT];
  192.           }
  193.         if (current_item[CONTEXT] > max_item[CONTEXT])
  194.           {
  195.             result = FALSE;
  196.             current_item[CONTEXT] = last_good_item;
  197.           }
  198.       }
  199.     else
  200.       {
  201.         for (i = 0; i > move; --i)
  202.           {
  203.             while (--current_item[CONTEXT] >= min_item[CONTEXT] &&
  204.                 ! get_subset_bit ((long)
  205.                     get_ptr_rec (current_item[CONTEXT])));
  206.             if (current_item[CONTEXT] < min_item[CONTEXT])
  207.                 break;
  208.             last_good_item = current_item[CONTEXT];
  209.           }
  210.         if (current_item[CONTEXT] < min_item[CONTEXT])
  211.           {
  212.             result = FALSE;
  213.             current_item[CONTEXT] = last_good_item;
  214.           }
  215.       }
  216.     return (result);
  217.   }
  218.   
  219.